/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.DccrDAO;
import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Dccr;
import com.cashmate.hibernate.pojo.Employees;
import com.cashmate.utilities.DCCRDate;
import com.opensymphony.xwork2.ActionSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Dean
 */
@Action(value = "/select-cutoff", results = {
    @Result(name = "success", type = "json"),
    @Result(name = "error", type = "redirect", location = "../error/messages.jsp")
})
public class ForSelectCutOff extends ActionSupport {

    private Logger logger = Logger.getLogger(ForSelectCutOff.class);
    private final DccrDAO dccrdao = (DccrDAO) ServletActionContext.getServletContext().getAttribute("dccrDAO");
    private final Branches b = (Branches) ServletActionContext.getRequest().getSession().getAttribute("branch");
    private Map<String, String> cutoffMap;

    @Override
    public String execute() throws Exception {
        try {
            DetachedCriteria criteria = DetachedCriteria.forClass(Dccr.class);
            cutoffMap = new HashMap<String, String>();

            criteria.add(Restrictions.eq("isactive", true));
            if (!b.getBranchcode().equals("HO")) {
                criteria.add(Restrictions.eq("branchid", b.getBranchid()));
            }
            int count = dccrdao.countByCriteria(criteria);
            criteria.setProjection(null);
            criteria.setResultTransformer(Criteria.ROOT_ENTITY);

            if (count > 0) {
                List<Dccr> dccrList = dccrdao.findByCriteria(criteria, 0, count);
                for (Dccr d : dccrList) {
                    DCCRDate date = new DCCRDate();
                    date.setDate(d.getDategenerated());
                    if (date.isIsCutOff() && !cutoffMap.containsKey(d.getDategenerated())) {
                        cutoffMap.put(d.getBranchid() + "-" + d.getDategenerated(), d.getBranchcode() + " - " + d.getDategenerated());
                    }
                }
            } else {
                cutoffMap.put("-1", "Empty");
            }
        } catch (Exception ex) {
            Employees emps = (Employees) ServletActionContext.getRequest().getSession().getAttribute("employee");
            if (emps != null) {
                logger.fatal("(" + emps.getIdnumber() + "):" + ex.getLocalizedMessage(), ex);
            } else {
                logger.fatal(ex.getLocalizedMessage(), ex);
            }
            addActionError(ex.getLocalizedMessage());
            return ERROR;
        } finally {
            DccrDAO.close();
        }

        return SUCCESS;
    }

    public Map<String, String> getCutoffMap() {
        return cutoffMap;
    }
}
